/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentation;

import BLO.DoibongBLO;
import BLO.LichTDBLO;
import DAO.ResultsModel;
import DTO.LICHTD;
import DTO.TRANDAU;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.generic.TABLESWITCH;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author LUCKYSTARS
 */
public class KetquaXephang extends javax.swing.JPanel {
DoibongBLO dbBLO =new DoibongBLO();
LichTDBLO ltdBLO= new LichTDBLO();
    /**
     * Creates new form KetquaXephang
     */
class SelectionListener implements ListSelectionListener {
    JTable table;
    SelectionListener(JTable table) {
    this.table = table;}
    public void valueChanged(ListSelectionEvent e) {
          int row= table.getSelectedRow();
          if(row>=0)
          {
           String tiso=table.getValueAt(row,3).toString().trim();
           String tiso1="";
           String tiso2="";
           txtMatran.setText(table.getValueAt(row,6).toString().trim());
           if(tiso.length()>0)
           {
               int vt=tiso.indexOf("-");
               //System.out.print(vt);
           tiso1 =tiso.substring(0,vt);
            tiso2=tiso.substring(vt+1);
           }
           txtTiso1.setText(tiso1);
           txtTiso2.setText(tiso2);

          }
    }
  }  
public KetquaXephang() {
        initComponents();
          //Add du lieu vao so vong 
          ResultSet dtDoibong=dbBLO.Getdata1();
        int sodoibong=0;
        try {
            //Add du lieu vao cmb Doi nha va cmb Doi khach
            while (dtDoibong.next()) 
            {
                sodoibong++;
            }
            for(int i=1;i<(sodoibong-1)*2+1;i++)
                cmbVongdau.addItem(i);
            
           int v=cmbVongdau.getSelectedIndex()+1;
            ResultsModel dtable= ltdBLO.GetKetqua(v);
            JTable tb= new JTable(dtable);
            tb.setBounds(0,0,869,200);
            JScrollPane js= new JScrollPane(tb);
            js.setBounds(0,0,869,200);
            jtableKetqua.removeAll();
            jtableKetqua.add(js);
            
             SelectionListener listener = new SelectionListener(tb);
             tb.getSelectionModel().addListSelectionListener(listener);
            
        }
        catch(Exception ex)
        {
             JOptionPane.showMessageDialog(null, ex.getMessage(),"Thông báo",JOptionPane.INFORMATION_MESSAGE);  
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbVongdau = new javax.swing.JComboBox();
        txtTiso1 = new javax.swing.JTextField();
        txtTiso2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnCapnhat = new javax.swing.JButton();
        btntrove = new javax.swing.JButton();
        txtMatran = new javax.swing.JTextField();
        jtableKetqua = new javax.swing.JPanel();

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setText("KẾT QUẢ THI ĐẤU CÁC VÒNG ");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
        jLabel2.setText("Vòng đấu:");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
        jLabel3.setText("Cập nhật tỉ số:");

        cmbVongdau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbVongdauActionPerformed(evt);
            }
        });

        txtTiso1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        txtTiso1.setToolTipText("");

        txtTiso2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel4.setText("-");

        btnCapnhat.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        btnCapnhat.setText("Cập nhật");
        btnCapnhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapnhatActionPerformed(evt);
            }
        });

        btntrove.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        btntrove.setText("Trở về");
        btntrove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntroveActionPerformed(evt);
            }
        });

        txtMatran.setEditable(false);
        txtMatran.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        txtMatran.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(213, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtTiso1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4))
                    .addComponent(cmbVongdau, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtMatran, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                    .addComponent(txtTiso2))
                .addGap(58, 58, 58)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btntrove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCapnhat, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                .addGap(177, 177, 177))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbVongdau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCapnhat, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMatran, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(txtTiso1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTiso2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btntrove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );

        txtTiso1.getAccessibleContext().setAccessibleDescription("");

        jtableKetqua.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jtableKetquaLayout = new javax.swing.GroupLayout(jtableKetqua);
        jtableKetqua.setLayout(jtableKetquaLayout);
        jtableKetquaLayout.setHorizontalGroup(
            jtableKetquaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jtableKetquaLayout.setVerticalGroup(
            jtableKetquaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 286, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(252, 252, 252)
                .addComponent(jLabel1)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jtableKetqua, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jtableKetqua, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmbVongdauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbVongdauActionPerformed
        
            try {
            int v = cmbVongdau.getSelectedIndex() + 1;
            
            ResultsModel dtable = ltdBLO.GetKetqua(v);
            JTable tb = new JTable(dtable);
            tb.setBounds(0, 0, 869, 200);
            JScrollPane js = new JScrollPane(tb);
            js.setBounds(0, 0, 869, 200);
            jtableKetqua.removeAll();
            jtableKetqua.add(js);
            
            SelectionListener listener = new SelectionListener(tb);
            tb.getSelectionModel().addListSelectionListener(listener);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Thong bao",JOptionPane.INFORMATION_MESSAGE);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbVongdauActionPerformed

    private void btnCapnhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapnhatActionPerformed
        // TODO add your handling code here:
        try {
            LICHTD ltdDTO = new LICHTD();
            TRANDAU tdDTO = new TRANDAU();
            tdDTO.setTiso(txtTiso1.getText().trim() + "-" + txtTiso2.getText().trim());
            tdDTO.setMatrandau(txtMatran.getText().trim());
            
            ltdDTO.setTrandau(tdDTO);
            
            ltdBLO.UpdateKetqua(ltdDTO);
            
            int v = cmbVongdau.getSelectedIndex() + 1;
            
            ResultsModel dtable = ltdBLO.GetKetqua(v);
            JTable tb = new JTable(dtable);
            tb.setBounds(0, 0, 869, 200);
            JScrollPane js = new JScrollPane(tb);
            js.setBounds(0, 0, 869, 200);
            jtableKetqua.removeAll();
            jtableKetqua.add(js);
            
            SelectionListener listener = new SelectionListener(tb);
            tb.getSelectionModel().addListSelectionListener(listener);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Thong bao",JOptionPane.INFORMATION_MESSAGE);
        }
    

    }//GEN-LAST:event_btnCapnhatActionPerformed

    private void btntroveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btntroveActionPerformed

            this.setVisible(false);
            // TODO add your handling code here:
    }//GEN-LAST:event_btntroveActionPerformed
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapnhat;
    private javax.swing.JButton btntrove;
    private javax.swing.JComboBox cmbVongdau;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jtableKetqua;
    private javax.swing.JTextField txtMatran;
    private javax.swing.JTextField txtTiso1;
    private javax.swing.JTextField txtTiso2;
    // End of variables declaration//GEN-END:variables
}
